package com.shermanzhou.juel.contition2;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @author zhouchaoqun02
 * @date 2020-10-25 17:42
 */
public class ConditionJsonDeserializer extends JsonDeserializer<ConditionTreeNode> {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public ConditionTreeNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode jsonNode = p.getCodec().readTree(p);
        String logical = jsonNode.get("logical").asText();
        if (logical == null  || logical.isEmpty()) {
            return objectMapper.treeToValue(jsonNode, ConditionLeafNode.class);
        }
        return objectMapper.treeToValue(jsonNode, ConditionSetNode.class);
    }
}
